package com.example.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.admin.common.contants.SystemConts;
import com.example.admin.common.result.R;
import com.example.admin.common.util.JwtUtil;
import com.example.admin.common.util.UserContext;
import com.example.admin.entity.NavGroup;
import com.example.admin.entity.NavLink;
import com.example.admin.entity.SysUser;
import com.example.admin.mapper.SysUserMapper;
import com.example.admin.model.dto.NavGroupDTO;
import com.example.admin.model.dto.NavLinkRequest;
import com.example.admin.service.NavService;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Api(tags = "导航管理接口")
@RestController
@RequestMapping("/api/nav")
@CrossOrigin
public class NavController {

    @Autowired
    private NavService navService;

    @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    private SysUserMapper userMapper;

    /**
     * 获取导航分组列表
     */
    @ApiOperation("获取导航分组列表")
    @GetMapping("/group/list")
    public R<List<NavGroup>> getNavGroupList() {
        return R.ok(navService.getNavGroups(UserContext.getUser().getId()));
    }

    /**
     * 新增导航分组
     */
    @ApiOperation("新增导航分组")
    @PostMapping("/group/add")
    public R<Void> addNavGroup(@RequestBody NavGroup navGroup) {
        navGroup.setUserId(UserContext.getUser().getId());
        navService.addNavGroup(navGroup);
        return R.ok();
    }

    /**
     * 更新导航分组
     */
    @ApiOperation("更新导航分组")
    @PostMapping("/group/update")
    public R<Void> updateNavGroup(@RequestBody NavGroup navGroup) {
        // 验证分组所有权
        navService.validateGroupOwnership(navGroup.getId(), UserContext.getUser().getId());
        navService.updateNavGroup(navGroup);
        return R.ok();
    }

    /**
     * 删除导航分组
     */
    @ApiOperation("删除导航分组")
    @GetMapping("/group/delete")
    public R<Void> deleteNavGroup(@ApiParam("分组ID") @RequestParam Integer id) {
        // 验证分组所有权
        navService.validateGroupOwnership(id, UserContext.getUser().getId());
        navService.deleteNavGroup(id);
        return R.ok();
    }

    /**
     * 分页查询导航链接列表
     */
    @ApiOperation("分页查询导航链接列表")
    @PostMapping("/link/list")
    public R<IPage<NavLink>> getNavLinkList(@RequestBody NavLinkRequest params) {
        return R.ok(navService.getNavLinks(params));
    }

    /**
     * 新增导航链接
     */
    @ApiOperation("新增导航链接")
    @PostMapping("/link/add")
    public R<Void> addNavLink(@RequestBody NavLink navLink) {
        navService.validateGroupOwnership(navLink.getGroupId(), UserContext.getUser().getId());
        navService.addNavLink(navLink);
        return R.ok();
    }

    /**
     * 更新导航链接
     */
    @ApiOperation("更新导航链接")
    @PostMapping("/link/update")
    public R<Void> updateNavLink(@RequestBody NavLink navLink) {
        navService.validateGroupOwnership(navLink.getGroupId(), UserContext.getUser().getId());
        navService.updateNavLink(navLink);
        return R.ok();
    }

    /**
     * 删除导航链接
     */
    @ApiOperation("删除导航链接")
    @GetMapping("/link/delete")
    public R<Void> deleteNavLink(@RequestParam Integer id) {
        navService.deleteNavLink(id);
        return R.ok();
    }

    /**
     * 获取导航数据
     * 未登录用户获取默认导航
     * 已登录用户获取个人导航
     */
    @ApiOperation("获取导航数据")
    @GetMapping("/home")
    public R<List<NavGroupDTO>> getNavData(HttpServletRequest request) {
        // 从请求头获取token
        String token = request.getHeader(SystemConts.AUTH_KEY);
        Long userId = null;

        // 如果有token,则获取用户ID
        if (StringUtils.hasText(token)) {
            try {
                String username = jwtUtil.getUsernameFromToken(token);
                if(username != null){
                    SysUser user = userMapper.selectOne(
                            new LambdaQueryWrapper<SysUser>()
                                    .eq(SysUser::getUsername, username)
                    );
                    userId = user.getId();
                }
            } catch (Exception e) {
            }
        }
        
        // 获取导航数据
        List<NavGroupDTO> navData = navService.getNavData(userId);
        return R.ok(navData);
    }
} 